<?php
namespace Servidor;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\MvcEvent;

class Module implements AutoloaderProviderInterface
{
    
    /**
     * Bootstrap
     * @param MvcEvent $e
     */
    public function onBootstrap(MvcEvent $e) {
        
    	$moduleManager = $e->getApplication()->getServiceManager()->get('modulemanager');
    	$sharedEvents = $moduleManager->getEventManager()->getSharedManager();
    
//     	$sharedEvents->attach(
//     			'Zend\Mvc\Controller\AbstractRestfulController',
//     			MvcEvent::EVENT_DISPATCH,
//     			array( $this, 'checkAuth'),
//     			-50
//     	);
    	
    	$sharedEvents->attach(
    			'Zend\Mvc\Controller\AbstractRestfulController',
    			MvcEvent::EVENT_DISPATCH,
    			array( $this, 'postProcess'),
    			-100
    	);
    }
    
    /**
     * Configurações do autoloader
     * (non-PHPdoc)
     * @see \Zend\ModuleManager\Feature\AutoloaderProviderInterface::getAutoloaderConfig()
     */
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
                ),
            ),
        );
    }

    
    /**
     * Retorna as configurações do módulo (Rotas, Services, Controllers)
     */
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    
    
    
    
    public function checkAuth( MvcEvent $e ) {
        
//         $headers = $e->getRequest()->getHeaders()->toArray();
        
//         if ( !isset( $headers['X-Token'] ) )
//             return $e->getResponse()->setStatusCode( 405 );
        
//         $token = $headers['X-Token'];
    }
    
    
    
    /**
     * @method postProcess
     * Método que processa a requisição Http
     * @param MvcEvent $e
     * @return NULL|$vars
     */
    public function postProcess( MvcEvent $e ) {
        
        $routeMatch = $e->getRouteMatch();
        $formatter = $routeMatch->getParam('formatter', false);
        
        $di = $e->getTarget()->getServiceLocator()->get('di');
        
        if ( $formatter !== false ) {
            
            if ( $e->getResult() instanceof \Zend\View\Model\ViewModel ) {
                
                if ( is_array( $e->getResult()->getVariables() ) )
                    $vars = $e->getResult()->getVariables();
                else
                    $vars = null; 
                    
            }
            else { 
                $vars = $e->getResult();
            }
            
            
            $postProcessor = $di->get( $formatter . '-pp', array(
            		'response'   => $e->getResponse(),
            		'vars'       => $vars,
            		'serializer' => $e->getTarget()->getServiceLocator()->get('jms_serializer.serializer')
            ) );
            
            $postProcessor->process();
            
            return $postProcessor->getResponse();
        }
        
        return null;
    }
    
    
    
    /**
     * @method getServiceConfig
     * Configurações de Serviços registrados no módulo
     * @return multitype:multitype:NULL
     */
    public function getServiceConfig() {
    
    	return array(
    			'factories' => array(    						
    				 # ATIVIDADE SERVICE ##################################################################
    			     'Servidor\Service\AtividadeService' => 'Servidor\ServiceFactory\AtividadeServiceFactory',
			     ));
    
    }
}
